/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.task;

import cn.hutool.core.collection.CollectionUtil;
import com.medusa.gruul.integral.api.entity.IntegralClock;
import com.medusa.gruul.integral.api.entity.IntegralLogIn;
import com.medusa.gruul.integral.api.entity.IntegralShop;
import com.medusa.gruul.integral.service.*;
import com.medusa.gruul.integral.web.enums.IntegralRuleGainTypeEnum;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: 把用户连续签到天数或者连续登入天数 大于商家后台设置的天数 在定时任务中 修改用户状态 为1L
 * @Author: xiaoq
 * @Date : 2020/1/3 9:57
 */
@JobHandler(value = "ReturnToZoreJobJob")
@Component
@Slf4j
public class ReturnToZoreJob extends IJobHandler {

	@Autowired
	private IIntegralShopService integralShopService;

	@Autowired
	private IIntegralRuleService integralRuleService;

	@Autowired
	private IIntegralClockService integralClockService;

	@Autowired
	private IIntegralEnterService integralEnterService;



	@Override
	public ReturnT<String> execute(String s) throws Exception {
		int signInStatus = IntegralRuleGainTypeEnum.SING_IN.getStatus();
		int longInStatus = IntegralRuleGainTypeEnum.LONG_IN.getStatus();
		// 1.获取商铺信息
		List<IntegralShop> allShopInfo = integralShopService.getAllShopInfo();
		log.info("----------- 商铺信息+" + allShopInfo.toString() + "+-------------");
		for (IntegralShop integralShop : allShopInfo) {
			// 2. 获取商铺设置的 最大签到天数
			int maxDays = integralRuleService.getShopInfo(integralShop, signInStatus);
			log.info("----------- 最大签到天数+" + maxDays + "+-------------");
			//2.1 获取用户连续签到天数大于商铺的签到天数 归0 连续签到天数等于=1L
			List<IntegralClock> integralClocks = integralClockService.selectShopAll(integralShop.getShopId());
			if (CollectionUtil.isNotEmpty(integralClocks)) {
				for (IntegralClock integralClock : integralClocks) {
					if (integralClock.getRunningDays() > maxDays) {
						integralClock.setRunningDays(1L);
						try {
							integralClockService.updateRunningDays(integralClock);
						} catch (Exception e) {
							log.error("需要更新id:".concat(integralClock.toString()) + e.getMessage(), e);
						}
					}
				}
			}
			maxDays = integralRuleService.getShopInfo(integralShop, longInStatus);
			log.info("----------- 最大登入天数+" + maxDays + "+-------------");
			List<IntegralLogIn> integralEnters = integralEnterService.selectShopAll(integralShop.getShopId());
			if (CollectionUtil.isNotEmpty(integralEnters)) {
				for (IntegralLogIn integralEnter : integralEnters) {
					if (integralEnter.getRunningDays() > maxDays) {
						integralEnter.setRunningDays(1L);
						try {
							integralEnterService.updateEnterDays(integralEnter);
						} catch (Exception e) {
							log.error("需要更新id:".concat(integralEnter.toString()) + e.getMessage(), e);
						}
					}
				}
			}
		}

		return SUCCESS;
	}
}
